package com.azkaban;

import com.alibaba.fastjson.JSONObject;
import com.azkaban.api.AzkabanApi;
import com.azkaban.response.BaseResponse;
import com.azkaban.response.ScheduleCronFlowResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.nio.charset.StandardCharsets;

/**
 * Created by shirukai on 2019-06-12 14:39
 * SpringBoot应用启动入口
 */
@SpringBootApplication
@Slf4j
public class Application implements CommandLineRunner {

    @Autowired
    private AzkabanApi azkabanApi;

    @Value("${azkaban.zip.file}")
    private String path;

    @Value("${azkaban.project.description}")
    private String description;

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.setRegisterShutdownHook(true);
        application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("azkaban zip file : {}",path);
        String azkabanProjectName = "flowai-warehouse";
        description = new String(description.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
        log.info(description);
        if ("install".equals(args[0])){
            log.info("createProject");
            BaseResponse createProject = azkabanApi.createProject(azkabanProjectName, description);
            log.info(JSONObject.toJSONString(createProject));
            log.info("uploadProjectZip");
            BaseResponse uploadProjectZip = azkabanApi.uploadProjectZip(path, azkabanProjectName);
            log.info(JSONObject.toJSONString(uploadProjectZip));
            log.info("scheduleCronFlow-daily");
            ScheduleCronFlowResponse scheduleFlowDailyResponse = azkabanApi.scheduleCronFlow(azkabanProjectName, "daily", "0+30+0+*+*+?+");
            log.info(JSONObject.toJSONString(scheduleFlowDailyResponse));
            log.info("scheduleCronFlow-clean");
            ScheduleCronFlowResponse scheduleFlowCleanResponse = azkabanApi.scheduleCronFlow(azkabanProjectName, "clean", "0+0+2+*+*+?+");
            log.info(JSONObject.toJSONString(scheduleFlowCleanResponse));
            log.info("scheduleCronFlow-monthly");
            ScheduleCronFlowResponse scheduleMonthlyResponse = azkabanApi.scheduleCronFlow(azkabanProjectName, "monthly", "0+0+12+1+*+?+");
            log.info(JSONObject.toJSONString(scheduleMonthlyResponse));
            log.info("azkaban crontab install finish");
        }else if ("stop".equals(args[0])){
            log.info("deleteProject");
            BaseResponse deleteProject = azkabanApi.deleteProject(azkabanProjectName);
            log.info("{\"status\":\"success\"}");
        } else {
            log.error("参数参数错误，只支持 install  stop");
        }
    }
}
